﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.不透光
{
     public class btgmqy201:btgbass
    {

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sSet1">串口设置</param>
        /// <returns>是否成功打开</returns>
        public override bool openCOM(string sSet1)
        {
            bool breturn = base.openCOM(sSet1);
            commex.ReadTimeout = 500;
            commex.ReceivedBytesThreshold = 46;//达到50字节以后才开始触发串口
            commex.DataReceived += comGAS_DataReceived;
            return breturn;
        }

        /// <summary>
        /// 发送串口数据
        /// </summary>
        /// <param name="cmd">通信命令
        /// 1:抽样气命令;
        /// 2:停止动作命令;
        /// 3:实时数据获取命令;
        /// 4:反吹;
        /// 5:仪器调零命令;
        /// 6:实时状态获取命令;
        /// 7:抽空气命令;
        /// 8:手动反吹命令;
        /// 9:通校准气命令;
        /// 10:通检查气命令;
        /// 11:通零气命令;
        /// 12:自动反吹命令;
        /// 13:背景空气测定命令;
        /// 14:环境空气测定命令;
        /// 15:泄漏检查命令;
        /// 16:预热期间进入系统命令;
        /// 17:背景气体数据获取命令;
        /// 18:环境气体数据获取命令;
        /// 19:按键锁止命令;
        /// 20:按键解锁命令;
        /// 21:清除烟度计最大值命令;
        /// 22:获取烟度计最大值命令;
        /// 23:烟度计调零命令;
        /// 24:气体调零命令;</param>
        /// <returns></returns>
        public override bool SendDeviceCmd(int cmd)
        {
            switch (cmd)
            {
                case 1: { objzsy.SendDeviceCmd(); break; }    // 1:抽样气命令;
                case 2: { break; }    // 2:停止动作命令;
                case 3: { SendDeviceCmdSK(); break; }    // 3:实时数据获取命令;
                case 4: { break; }    // 4:反吹;
                case 5: { break; }    // 5:仪器调零命令;
                case 6: { break; }    // 6:实时状态获取命令;
                case 7: { break; }    // 7:抽空气命令;
                case 8: { break; }    // 8:手动反吹命令;
                case 9: { break; }    // 9:通校准气命令;
                case 10: { break; }   // 10:通检查气命令;
                case 11: { break; }   // 11:通零气命令;
                case 12: { break; }   // 12:自动反吹命令;
                case 13: { break; }   // 13:背景空气测定命令;
                case 14: { break; }   // 14:环境空气测定命令;
                case 15: { break; }   // 15:泄漏检查命令;
                case 16: { break; }   // 16:预热期间进入系统命令;
                case 17: { break; }   // 17:背景气体数据获取命令;
                case 18: { break; }   // 18:环境气体数据获取命令;
                case 19: { break; }   // 19:按键锁止命令;
                case 20: { break; }   // 20:按键解锁命令;
                case 21: { break; }     // 21:清除烟度计最大值命令;
                case 22: { break; }     // 22:获取烟度计最大值命令;
                case 23: { break; }     // 23:烟度计调零命令;
                case 24: { break; }     // 24:气体调零命令;
            }

            return true;
        }


        private bool SendDeviceCmdSK()
        {
            byte[] cCmdBuf = new byte[2] { 0xAE, 0x52 };
            cCmdBuf[0] = 0xAE;
            cCmdBuf[1] = 0x52;
            if (commex.IsOpen)
            {
                Byte[] m_byte = new Byte[2];
                for (int i = 0; i < 2; i++)
                {
                    m_byte[i] = cCmdBuf[i];
                }
                commex.Write(m_byte, 0, 2);
            }
            return true;
        }
        private bool sendcmd(byte cCmd2, byte cCmd4)
        {
            byte[] cCmdBuf = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
            cCmdBuf[0] = 0x05;
            cCmdBuf[1] = cCmd2;
            cCmdBuf[2] = 0x03;
            cCmdBuf[3] = cCmd4;//
            if (commex.IsOpen)
            {
                Byte[] m_byte = new Byte[4];
                for (int i = 0; i < 4; i++)
                {
                    m_byte[i] = cCmdBuf[i];
                }
                commex.Write(m_byte, 0, 4);
            }
            return true;
        }
        private List<byte> buffer1 = new List<byte>(4096);
        void comGAS_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            //if (commex.IsOpen)
            //{
            //    int n = commex.BytesToRead;
            //    byte[] buf = new byte[n];
            //    commex.Read(buf, 0, n);

            //    for (int i = 0; i < n - 9; i++)
            //    {
            //        if (buf[0 + i] == 0xA6)
            //        {
            //            byte uSumSK = 0;
            //            for (int ff = 0; ff < 9; ff++)
            //            {
            //                uSumSK += (byte)(buf[ff + i]);
            //            }
            //            uSumSK = (byte)(~uSumSK + 1);
            //            short uSum = 0;
            //            uSum = (short)(buf[i + 9]);
            //            if (uSumSK == uSum)
            //            {
            //                m_fsk = 0;
            //                float.TryParse(string.Format("{0}", (buf[3 + i] * 256 + buf[4 + i]) * 0.01), out m_fsk);
            //                float.TryParse(string.Format("{0}", (buf[7 + i] * 256 + buf[8 + i]) * 0.01), out m_fyouwenSK);

            //                if (m_fsk > 16)
            //                {
            //                    m_fsk = 16.01f;
            //                }
            //                //bcomsk = true;
            //            }
            //        }
            //    }
            //}

            if (commex.IsOpen)
            {
                int n = commex.BytesToRead;//
                byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                byte[] buf = new byte[n];
                commex.Read(buf1, 0, n);
                if (n > 14)
                {
                    buf = null;
                    buf = new byte[n];
                    buf1.CopyTo(buf, 0);
                    for (int i = 0; i < n - 14; i++)
                    {
                        if (buf[0 + i] == 0xAE)
                        {
                            byte uSumSK = 0;
                            for (int ff = 0; ff < 15; ff++)
                            {
                                uSumSK += (byte)(buf[ff + i]);
                            }
                            uSumSK = (byte)(~uSumSK + 1);
                            short uSum = 0;
                            uSum = (short)(buf[i + 15]);
                            if (uSumSK == uSum)
                            {
                                // m_fsk = (buf[3] * 256 + buf[4]) / 100;
                                float.TryParse(string.Format("{0}", (buf[3 + i] * 256 + buf[4 + i]) * 0.01), out m_fsk);
                                //m_iGasturn = buf[5 + i] * 256 + buf[6 + i];
                                // m_fyaliSK = (buf[9] * 256 + buf[10]) / 100;
                                float.TryParse(string.Format("{0}", (buf[9 + i] * 256 + buf[10 + i]) * 0.01), out m_fYaLi);
                                if (buf[11 + i] < 0x10)
                                {
                                    // m_fwenduSK = (buf[11] * 256 + buf[12]) / 10;
                                    float.TryParse(string.Format("{0}", (buf[11 + i] * 256 + buf[12 + i]) * 0.1), out m_fWenDu);
                                }
                                else
                                {
                                    int iwd = buf[11 + i] - 0x10;
                                    //  m_fwenduSK = 0 - ((iwd * 256 + buf[12]) / 10);
                                    float.TryParse(string.Format("{0}", (iwd * 256 + buf[12 + i]) * 0.1), out m_fWenDu);
                                    m_fWenDu = 0 - m_fWenDu;

                                }
                                // m_fshiduSK =  (buf[13] * 256 + buf[14]) / 10;
                                float.TryParse(string.Format("{0}", (buf[13 + i] * 256 + buf[14 + i]) * 0.1), out m_fShiDu);
                            }
                        }
                    }
                }
            }
            //-------------------------------

        }
    }
}
